package com.oocl.o2o.client.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.client.entity.OrderItem;
import com.oocl.o2o.client.service.OrderItemService;
import com.oocl.o2o.client.vo.OrderItemVO;

/**
 * @description: 
 * @author  XIEMI2
 * @date Aug 13, 2017 11:47:58 AM
 * @version 1.0
 */
@Controller
@RequestMapping("/orderItems")
public class OrderItemController {
	
	@Resource
	private OrderItemService orderItemService;
	
	@PostMapping
	public void addOrderItem(@RequestBody OrderItemVO orderItemVO, HttpSession session){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems = orderItemService.addOrderItem(orderItems, orderItemVO);
		session.setAttribute("orderItems", orderItems);
	}
	
	
	@ResponseBody
	@GetMapping
	public Map<String, OrderItemVO> getAllOrderItems(HttpSession session){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		return orderItems;
	}
	

	@ResponseBody
	@PatchMapping
	public Map<String, OrderItemVO> plusOneItem(HttpSession session, @RequestBody OrderItemVO orderItemVO){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems = orderItemService.plusOneItem(orderItems, orderItemVO);
		return orderItems;
	}
	
	@ResponseBody
	@DeleteMapping
	public Map<String, OrderItemVO> minusOneItem(HttpSession session, @RequestBody OrderItemVO orderItemVO){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems = orderItemService.minusOneItem(orderItems, orderItemVO);
		return orderItems;
	}
	
	@ResponseBody
	@DeleteMapping("/{foodId}")
	public Map<String, OrderItemVO> removeOneItem(HttpSession session, @PathVariable("foodId") String foodId){
		System.out.println("待移除的foodId " + foodId);
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems = orderItemService.removeOneItem(orderItems, foodId);
		session.setAttribute("orderItems", orderItems);
		return orderItems;
	}
	
	@ResponseBody
	@DeleteMapping("/all")
	public Map<String, OrderItemVO> removeAllItems(HttpSession session){
		Map<String, OrderItemVO> orderItems = new HashMap<String, OrderItemVO>();
		session.setAttribute("orderItems", orderItems);
		return orderItems;
	}
	
	@ResponseBody
	@PatchMapping("/{foodId}")
	public Map<String, OrderItemVO> plusOneItemOrderInfo(HttpSession session,@PathVariable("foodId") String foodId){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems.get(foodId).setTotal(orderItems.get(foodId).getTotal()+1);
		return orderItems;
	}
	
	@ResponseBody
	@DeleteMapping("/foodId/{foodId}")
	public Map<String, OrderItemVO> minusOneItemOrderInfo(HttpSession session,@PathVariable("foodId") String foodId){
		Map<String, OrderItemVO> orderItems = (Map<String, OrderItemVO>) session.getAttribute("orderItems");
		orderItems.get(foodId).setTotal(orderItems.get(foodId).getTotal()-1);
		return orderItems;
	}
}


